package com.alibaba.citrus.ots.platform.enhance.service.impl;

import com.alibaba.citrus.ots.platform.enhance.service.ScItemMeshFlowService;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.fastjson.JSON;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsplatformenhance.api.scitemquery.service.ScItemQueryService;
import com.epoch.app.otsplatformenhance.api.scitemwrite.service.ScItemWriteService;
import com.epoch.app.otsplatformenhance.dto.feature.ScItemFeatures;
import com.epoch.app.otsplatformenhance.dto.request.CreateScItemRequestNew;
import com.epoch.app.otsplatformenhance.dto.request.EditScItemRelCategoryRequest;
import com.epoch.app.otsplatformenhance.dto.request.EditScItemRequest;
import com.epoch.app.otsplatformenhance.dto.request.IdQueryRequest;
import com.epoch.app.otsplatformenhance.dto.request.QueryScItemRequest;
import com.epoch.app.otsplatformenhance.dto.response.ScItemDetailResponse;
import com.epoch.app.otsplatformenhance.dto.response.ScItemResponse;
import com.epoch.app.otsplatformenhance.model.dto.ScItemSDO;
import com.epoch.app.otsplatformenhance.model.enums.ScItemMarkTypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zls
 * @description: TODO
 * @date 2021/9/8 19:05
 */
@Component
public class ScItemMeshFlowServiceImpl implements ScItemMeshFlowService {

    @Resource
    ScItemQueryService scItemQueryService;
    @Resource
    ScItemWriteService scItemWriteService;

    @Override
    public Result<Boolean> createScItem(CreateScItemRequestNew request) {
//        return scItemWriteService.createScItem(request);
        return null;
    }

    @Override
    public Result<Boolean> editScItem(EditScItemRequest request) {
        return scItemWriteService.editScItem(request);
    }

    @Override
    public Result<Boolean> editScItemRelCategory(EditScItemRelCategoryRequest request) {
        ScItemSDO scItemSDO = new ScItemSDO();
        BeanUtils.copyProperties(request, scItemSDO);
        return scItemWriteService.editScItemRelCategory(scItemSDO);
    }

    @Override
    public Result<ScItemDetailResponse> getScItemDetail(IdQueryRequest request) {
        //  查询货品,查询品牌,查询类目,一切相关的都查询
        Result<ScItemSDO> scItemSDOResult = scItemQueryService.getScItemById(request);
        if (null != scItemSDOResult && null != scItemSDOResult.getResult()) {
            ScItemSDO result = scItemSDOResult.getResult();
            ScItemDetailResponse scItemDetailResponse = new ScItemDetailResponse();
            BeanUtils.copyProperties(result, scItemDetailResponse);
            Map<String, String> scItemMarkTypeMap = new HashMap<>();
            Arrays.stream(ScItemMarkTypeEnum.values()).forEach(value -> {
                scItemMarkTypeMap.put(value.getMarkType(), value.getDesc());
            });
            Map<String, String> features = result.getFeatures();
            //  feature 转对象
            ScItemFeatures scItemFeatures = JSON.parseObject(JSON.toJSONString(features), ScItemFeatures.class);
            BeanUtils.copyProperties(scItemFeatures, scItemDetailResponse);
            if (null != scItemFeatures.getScItemMarkType()) {
                scItemDetailResponse.setScItemMarkType(Optional.ofNullable(scItemMarkTypeMap.get(scItemFeatures.getScItemMarkType())).orElse(null));
            }
            scItemDetailResponse.setCategoryId(result.getLeafCategoryId());
            String scItemGroupDesc = scItemFeatures.getScItemGroupDesc();
            if (StringUtils.isNotBlank(scItemGroupDesc)) {
                scItemDetailResponse.setScItemGroupName(scItemGroupDesc);
            } else {
                scItemDetailResponse.setScItemGroupName("常温");
            }
            // 毛重和净重入表是*1000，这里要除以1000
            String netWeight = scItemFeatures.getNetWeight();
            if (StringUtils.isNotBlank(netWeight)) {
                String netWeightStr = new BigDecimal(netWeight).multiply(new BigDecimal(0.001)).setScale(3, BigDecimal.ROUND_HALF_UP).toString();
                scItemDetailResponse.setNetWeight(netWeightStr);
            }
            String grossWeight = scItemFeatures.getGrossWeight();
            if (StringUtils.isNotBlank(grossWeight))  {
                String grossWeightStr = new BigDecimal(grossWeight).multiply(new BigDecimal(0.001)).setScale(3, BigDecimal.ROUND_HALF_UP).toString();
                scItemDetailResponse.setGrossWeight(grossWeightStr);
            }
            return Result.success(scItemDetailResponse);
        }
       return Result.success(null);
    }

    @Override
    public Result<PageResult<List<ScItemResponse>>> pageQueryScItemList(QueryScItemRequest request) {
        Result<List<ScItemSDO>> pageResultResult = scItemQueryService.pageQueryScItemList(request);
        List<ScItemSDO> result = pageResultResult.getResult();
        Map<String, String> scItemMarkTypeMap = new HashMap<>();
        Arrays.stream(ScItemMarkTypeEnum.values()).forEach(value -> {
            scItemMarkTypeMap.put(value.getMarkType(), value.getDesc());
        });
        // 解析features
        List<ScItemResponse> collect = result.stream().map(scItemSDO -> {
            ScItemResponse scItemResponse = new ScItemResponse();
            BeanUtils.copyProperties(scItemSDO, scItemResponse);
            Map<String, String> features = scItemSDO.getFeatures();
            ScItemFeatures scItemFeatures = JSON.parseObject(JSON.toJSONString(features), ScItemFeatures.class);
            BeanUtils.copyProperties(scItemFeatures, scItemResponse);
            scItemResponse.setLeafCategoryId(scItemSDO.getLeafCategoryId());
            if (null != scItemFeatures.getScItemMarkType()) {
                scItemResponse.setScItemMarkType(Optional.ofNullable(scItemMarkTypeMap.get(scItemFeatures.getScItemMarkType())).orElse(null));
            }
            Integer status = scItemSDO.getStatus();
            if (status.equals(1)) {
                scItemResponse.setStatusDesc("可用");
            } else {
                scItemResponse.setStatusDesc("禁用");
            }
            String scItemGroupDesc = scItemFeatures.getScItemGroupDesc();
            if (StringUtils.isNotBlank(scItemGroupDesc)) {
                scItemResponse.setScItemGroupName(scItemGroupDesc);
            } else {
                scItemResponse.setScItemGroupName("常温");
            }
            scItemResponse.setBrandName(scItemFeatures.getBrandDesc());
            // 毛重和净重入表是*1000，这里要除以1000
            String netWeight = scItemFeatures.getNetWeight();
            if (StringUtils.isNotBlank(netWeight)) {
                String netWeightStr = new BigDecimal(netWeight).multiply(new BigDecimal(0.001)).setScale(3, BigDecimal.ROUND_HALF_UP).toString();
                scItemResponse.setNetWeight(netWeightStr);
            }
            String grossWeight = scItemFeatures.getGrossWeight();
            if (StringUtils.isNotBlank(grossWeight))  {
                String grossWeightStr = new BigDecimal(grossWeight).multiply(new BigDecimal(0.001)).setScale(3, BigDecimal.ROUND_HALF_UP).toString();
                scItemResponse.setGrossWeight(grossWeightStr);
            }
            return scItemResponse;
        }).collect(Collectors.toList());
        return Result.success(PageResult.of(collect,pageResultResult.getTotal()));
    }


    @Override
    public Result<PageResult<List<ScItemResponse>>> queryScItemListByPage(QueryScItemRequest request) {
        Result<List<ScItemSDO>> pageResultResult = scItemQueryService.pageQueryScItemListFromDB(request);
        List<ScItemSDO> result = pageResultResult.getResult();
        Map<String, String> scItemMarkTypeMap = new HashMap<>();
        Arrays.stream(ScItemMarkTypeEnum.values()).forEach(value -> {
            scItemMarkTypeMap.put(value.getMarkType(), value.getDesc());
        });
        // 解析features
        List<ScItemResponse> collect = result.stream().map(scItemSDO -> {
            ScItemResponse scItemResponse = new ScItemResponse();
            BeanUtils.copyProperties(scItemSDO, scItemResponse);
            Map<String, String> features = scItemSDO.getFeatures();
            ScItemFeatures scItemFeatures = JSON.parseObject(JSON.toJSONString(features), ScItemFeatures.class);
            BeanUtils.copyProperties(scItemFeatures, scItemResponse);
            scItemResponse.setLeafCategoryId(scItemSDO.getLeafCategoryId());
            if (null != scItemFeatures.getScItemMarkType()) {
                scItemResponse.setScItemMarkType(Optional.ofNullable(scItemMarkTypeMap.get(scItemFeatures.getScItemMarkType())).orElse(null));
            }
            Integer status = scItemSDO.getStatus();
            if (status.equals(1)) {
                scItemResponse.setStatusDesc("可用");
            } else {
                scItemResponse.setStatusDesc("禁用");
            }
            String scItemGroupDesc = scItemFeatures.getScItemGroupDesc();
            if (StringUtils.isNotBlank(scItemGroupDesc)) {
                scItemResponse.setScItemGroupName(scItemGroupDesc);
            } else {
                scItemResponse.setScItemGroupName("常温");
            }
            scItemResponse.setBrandName(scItemFeatures.getBrandDesc());
            // 毛重和净重入表是*1000，这里要除以1000
            String netWeight = scItemFeatures.getNetWeight();
            if (StringUtils.isNotBlank(netWeight)) {
                String netWeightStr = new BigDecimal(netWeight).multiply(new BigDecimal(0.001)).setScale(3, BigDecimal.ROUND_HALF_UP).toString();
                scItemResponse.setNetWeight(netWeightStr);
            }
            String grossWeight = scItemFeatures.getGrossWeight();
            if (StringUtils.isNotBlank(grossWeight))  {
                String grossWeightStr = new BigDecimal(grossWeight).multiply(new BigDecimal(0.001)).setScale(3, BigDecimal.ROUND_HALF_UP).toString();
                scItemResponse.setGrossWeight(grossWeightStr);
            }
            return scItemResponse;
        }).collect(Collectors.toList());
        return Result.success(PageResult.of(collect,pageResultResult.getTotal()));
    }


}
